﻿Imports System.Data
Imports System.Data.SqlClient
Public Class nkhachsan
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub
    Public Sub Datagrid_OnCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Public Sub Datagrid_OnEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        BindGrid()
    End Sub
    Public Sub Datagrid_OnUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim sSql As String
        Dim blnAddNew As Boolean = False
        Dim txttenks As TextBox
        Dim txtdiachi As TextBox
        Dim txtquan_huyen As TextBox
        Dim txttinh_tp As TextBox
        Dim txtdienthoai As TextBox
        Dim txthangsao As TextBox
        Dim txtresort As TextBox
        Dim txttinh_tp1 As TextBox
        Dim txtweb As TextBox
        Dim txtChuaTrangBi As TextBox

        '----------------Lay du lieu -------------
        txttenks = e.Item.Cells(2).Controls(0)
        txtdiachi = e.Item.Cells(3).Controls(0)
        txtquan_huyen = e.Item.Cells(4).Controls(0)
        txttinh_tp1 = e.Item.Cells(5).Controls(0)
        txtdienthoai = e.Item.Cells(6).Controls(0)
        txthangsao = e.Item.Cells(7).Controls(0)
        txtresort = e.Item.Cells(8).Controls(0)
        txttinh_tp = e.Item.Cells(9).Controls(0)
        txtweb = e.Item.Cells(10).Controls(0)
        txtChuaTrangBi = e.Item.Cells(11).Controls(0)


        '----------------Update du lieu -------------
        If e.Item.Cells(1).Text < 1 Then
            blnAddNew = True
        End If
        If blnAddNew = True Then
            ' Insert into
            sSql = " insert into Khachsan (tenks,diachi,quan_huyen,tinh_tp,dienthoai,hangsao,resort,tinh_tp1, web, chuatrangbi)" & _
            "values(@tenks,@diachi,@quan_huyen,@tinh_tp,@dienthoai,@hangsao,@resort,@tinh_tp1, @web, @chuatrangbi) "
        Else
            'Update
            sSql = " UPDATE Khachsan SET" & _
            " tenks =@tenks,diachi =@diachi, resort=@resort,tinh_tp1=@tinh_tp1 " & _
            ", quan_huyen =@quan_huyen" & _
            ", tinh_tp =@tinh_tp" & _
            ",  hangsao=@hangsao " & _
            ", dienthoai =@dienthoai" & _
            ", web =@web" & _
            ", chuatrangbi =@chuatrangbi" & _
            " WHERE idKhachsan = @idKhachsan"
        End If
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idKhachsan", e.Item.Cells(1).Text))
        End If
        If txthangsao.Text = "" Then txthangsao.Text = "0"

        MyCommand.Parameters.Add(New SqlParameter("@tenks", txttenks.Text))
        MyCommand.Parameters.Add(New SqlParameter("@diachi", txtdiachi.Text))
        MyCommand.Parameters.Add(New SqlParameter("@quan_huyen", txtquan_huyen.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tinh_tp", txttinh_tp.Text))
        MyCommand.Parameters.Add(New SqlParameter("@dienthoai", txtdienthoai.Text))
        MyCommand.Parameters.Add(New SqlParameter("@hangsao", txthangsao.Text))
        MyCommand.Parameters.Add(New SqlParameter("@resort", txtresort.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tinh_tp1", txttinh_tp1.Text))
        MyCommand.Parameters.Add(New SqlParameter("@web", txtweb.Text))
        MyCommand.Parameters.Add(New SqlParameter("@chuatrangbi", IIf(txtChuaTrangBi.Text.ToLower() = "true", "1", "0")))

        MyCommand.ExecuteNonQuery()
        myConn.Close()
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Private Sub BindGrid()
        Dim idTinhThanh As Int16 = -1
        If IsNumeric(Request.QueryString("c")) Then
            idTinhThanh = Request.QueryString("c")
        End If

        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sSql As String = "select *  from Khachsan order by idkhachsan desc"
        If Request.QueryString("k") <> "" Then
            sSql = "select *  from Khachsan where tenks + ' ' + diachi + ' ' + tinh_tp1 like N'%" & Request.QueryString("k") & "%' " & " order by idkhachsan desc "
        End If
        myConn.Open()
        Dim myAdapter As New SqlDataAdapter(sSql, myConn)
        Dim myDataSet As New DataSet
        myAdapter.Fill(myDataSet, sSql)
        dr = myDataSet.Tables(0).NewRow()
        dr(0) = -1
        dr(1) = "Thêm mới"
        myDataSet.Tables(0).Rows.InsertAt(dr, 0)
        DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
        myConn.Close()
    End Sub
    Public Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BindGrid()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Response.Redirect("nKhachsan.aspx?k=" & TextBox1.Text)
    End Sub
End Class
